global.Proxy = undefined;
var os = require("os");
if (os.type() === "Linux") {
	global._isDev = false;
} else {
	global._isDev = true;
}
//统一的过滤机制
var _filter_nojson_able_keys = function(key, obj) {
	if (obj) {
		var nojson_able_keys = obj.nojson_able_keys;
		var new_obj = obj instanceof Array ? [] : {};
		if (nojson_able_keys instanceof Array) {
			for (var i in obj) { //将数据从黑名单里面排除
				if (nojson_able_keys.indexOf(i) === -1) {
					new_obj[i] = obj[i];
				}
			}
			//返回过滤后的数据集合
			return new_obj;
		} else {
			for (var i in obj) {
				new_obj[i] = obj[i];
			}
		}
	}
	return obj
};
JSON._stringify = JSON.stringify;
JSON.stringifySecurity = function(obj, handle) {
	var arg = Array.prototype.slice.call(arguments);
	if (!handle) {
		arg[1] = _filter_nojson_able_keys;
	}
	return JSON._stringify.apply(JSON, arg);
};
Number.prototype.fixMoney = function() {
	return parseFloat(this.toFixed(6));
};
Date.prototype.format = function(format) {
	var o = {
		"M+": this.getMonth() + 1, //month
		"d+": this.getDate(), //day
		"H+": this.getHours(), //hour
		"m+": this.getMinutes(), //minute
		"s+": this.getSeconds(), //second
		"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
		"S": this.getMilliseconds() //millisecond
	};
	if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(format))
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
	return format;
};
Object.defineProperty(Object.prototype, "hasOwnValue", {
	enumerable: false,
	value: function(value) {
		var obj = this;
		for (var key in obj) {
			if (obj.hasOwnProperty(key) && obj[key] === value) {
				return true;
			}
		}
		return false;
	}
});
String.prototype.format = function(args) {
	var result = this;
	if (arguments.length > 0) {
		if (arguments.length == 1 && typeof(args) == "object") {
			for (var key in args) {
				if (args[key] != undefined) {
					var reg = new RegExp("({" + key + "})", "g");
					result = result.replace(reg, args[key]);
				}
			}
		} else {
			for (var i = 0; i < arguments.length; i++) {
				if (arguments[i] != undefined) {
					//var reg = new RegExp("({[" + i + "]})", "g");//这个在索引大于9时会有问题，谢谢何以笙箫的指出
					　　　　　　　　　　　　
					var reg = new RegExp("({)" + i + "(})", "g");
					result = result.replace(reg, arguments[i]);
				}
			}
		}
	}
	return result;
};
String.prototype.parseJSON = function() {
	return JSON.parse(this);
};
Object.defineProperty(Object.prototype, "multiInherits", {
	enumerable: false,
	value: function() {
		var args = Array.prototype.slice.call(arguments);
		var obj = this;
		args.forEach(function(extendObj) {
			for (var i in extendObj) {
				obj[i] = extendObj[i];
			}
		});
		return obj;
	}
});
var Fiber = require('fibers');
Fiber.prototype.bindRun = function() {
	this.run = this.run.bind(this);
};

Fiber._yield = Fiber.yield;
Fiber.yield = function() {
	var f = Fiber.current;
	if (f) {
		f.yielding = true;
		var _run = f.run;
		f.run = function() {
			f.yielding = false;
			return _run.apply(f, arguments);
		}
	}
	return Fiber._yield();
};
var http = require("http");
var https = require("https");

global.curl = function(url) {
	var fiber = Fiber.current;
	var result = "";
	var handle = http;
	if (url.indexOf("https://") === 0) {
		handle = https;
	}
	handle.get(url, function(res) {
		res.on("data", function(chunk) {
			result += chunk;
		}).on("end", function() {
			//继续
			fiber.run();
		})
	});
	Fiber.yield(); //挂起
	return result
};
Function.noop = function() {};
Function.Permission = function(matchFun, succFun, errFun) {
	//字符串模式
	if (typeof matchFun === "string") {
		var matchStr = matchFun;
		matchFun = Function("return " + matchStr);
	} else if (typeof matchFun !== "function") {
		throw new TypeError("Permission Argument Must Be String Or Function");
	}

	succFun || (succFun = Function.noop);

	if (typeof errFun === "string") {
		var errStr = errFun;
		errFun = function() {
			throw new Error(errStr);
		}
	} else if (typeof errFun !== "function") {
		errFun = Function.noop;
	}

	return function() {
		var args = Array.prototype.slice.call(arguments);
		if (matchFun.apply(this, args)) {
			return succFun.apply(this, args);
		} else {
			return errFun.apply(this, args);
		}
	}
};
Function.prototype.perm = Function.prototype.permission = function(matchFun, errFun) {
	return Function.Permission(matchFun, this, errFun);
};

global.throwCusError = function throwCusError(e) {
	if (!(e instanceof Error)) {
		e = Error(e);
	}
	// console.log("Fiber throwInto:", Fiber.current && Fiber.current.yielding);
	// console.log(Error().stack);
	// console.log("THROW ERROR:", e.stack, !!Fiber.current);
	// logDomain("throwCusError");
	if (Fiber.current && Fiber.current.yielding) {
		Fiber.current.throwInto(e);
		// return;
	} else {
		throw e;
	}
};
global.logDomain = function(name) {
	console.log("DOMAIN in " + (name || "") + ":" + !!process.domain)
};

module.exports = function(app) {}

//TEST
// a = {
// 	is_ok: false,
// };
// a.__proto__ = {
// 	dosomething: function() {
// 		console.log("is OK!!!");
// 	}.permission("this.is_ok", "权限不足，必须是高级会员用户才能使用")
// };
// a.is_ok = true;
// a.dosomething();